package com.xiaomi.front.controller;

import com.xiaomi.front.dao.Describes.DescribesDao;
import com.xiaomi.front.service.commoditys.CommService;
import com.xiaomi.front.service.shopp_cart.ShoppCartService;
import com.xiaomi.pojo.Commoditys;
import com.xiaomi.pojo.Describes;
import com.xiaomi.pojo.Shoppingcart;
import com.xiaomi.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommController {


    @Autowired
    private CommService commService;
    @Autowired
    private ShoppCartService shoppCartService;
    @Autowired
    private DescribesDao describesDao;

    //按商品编号查询商品详情
    @RequestMapping("/FindCid/{uid}")
    public String FindCid(@PathVariable int uid, Model model) {
        Commoditys commoditys = commService.FindComm(uid);

        if (commoditys == null) {
            return "forward:/commerr.jsp";
        } else {
            model.addAttribute("commlist", commoditys);
            return "front/details";
        }
    }

    //添加购物车
    @RequestMapping("/AddShoppCart/{id}")
    public String AddShoppCart(@PathVariable("id") int deid, Model model, HttpSession session) {

        //获取session中用户得id
        User loginUser = (User) session.getAttribute("user");

        //null 未登录
        if (loginUser != null) {
            //存入购物车
            int count = shoppCartService.ExitsCart(loginUser.getUid(), deid);
            //查询 添加得商品名称 规格
            Shoppingcart shoppingcart = shoppCartService.getCart(loginUser.getUid(), deid);
            model.addAttribute("shoppingcart", shoppingcart);
            //add_shoppcart 商品推荐
            List<Describes> describeslist = describesDao.GetAllRecommend();
            model.addAttribute("describeslist", describeslist);

            return "front/shoppcart/add_shoppcart";
        } else {
            return "forward:/front_login.jsp";
        }
    }
}
